<template>
  <el-container class="homeContainer">
    <el-header>
      <div class="title">
        <img src="../assets/css/logo.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <el-container>
      <!-- 将侧边栏折叠起来，动态数据绑定width -->
      <el-aside :width="isCollapse ?'64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">
          <span>|||</span>
        </div>
        <!--侧边栏菜单 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active='activePath'
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 标题 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavStatus('/'+subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 标题 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //左侧菜单数据
      menuList: [],
      //菜单栏图标
      iconObj:{
        '125':'el-icon-user-solid',
        '103':'el-icon-lock',
        '101':'el-icon-s-goods',
        '102':'el-icon-s-order',
        '145':'el-icon-s-platform'
      },
      isCollapse:false,
      //被激活的链接地址
      activePath:''
    }
  },
  created() {
    this.getMenuList()
    //将激活状态取出
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginOut() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.axios.get('menus')
      if (res.meta.status != 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      console.log(res)
    },
    toggleCollapse(){
      this.isCollapse = !this.isCollapse

    },
    //储存激活状态
    saveNavStatus(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    }
  },
}
</script>
<style>
.homeContainer {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.title {
  display: flex;
  align-items: center;
}
.title span {
  margin-left: 10px;
}
.el-aside {
  background-color: #333744;
  
}
.toggle-button{
  background-color: #4a5064;
  line-height: 25px;
  font-size: 10px;
  color: #fff;
  text-align: center;
  cursor: pointer;
  letter-spacing: 0.2em;
}
.el-aside .el-menu{
  border-right: none;
}
.el-main {
  background-color: #eaedf1;
}
img {
  height: 60px;
}

</style>